package cn.sczhyt.modules.sys.service.impl;

import cn.sczhyt.modules.sys.form.UserInfoForm;
import cn.sczhyt.modules.sys.form.UserForm;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.sczhyt.modules.sys.dao.SysUserDao;
import cn.sczhyt.modules.sys.entity.*;
import cn.sczhyt.modules.sys.entity.vo.RoleSearchEntity;
import cn.sczhyt.modules.sys.entity.vo.SysUserInfoVOEntity;
import cn.sczhyt.modules.sys.entity.vo.UserInfoVoEntity;
import cn.sczhyt.modules.sys.service.SysUserRoleService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.Query;

import cn.sczhyt.modules.sys.dao.SysUserInfoDao;
import cn.sczhyt.modules.sys.service.SysUserInfoService;
import org.springframework.transaction.annotation.Transactional;

import static cn.sczhyt.common.utils.ShiroUtils.getUserId;


@Service("sysUserInfoService")
public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoDao, SysUserInfoEntity> implements SysUserInfoService {

    @Autowired
    private SysUserDao userDao;

    @Autowired
    private SysUserInfoDao userInfoDao;


    @Autowired
    private SysUserRoleService userRoleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysUserInfoEntity> page = this.page(
                new Query<SysUserInfoEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public SysUserInfoEntity getByUserId(String userId) {


        return userInfoDao.selectByUserId(userId);
    }

//    @Override
//    public List<SysUserInfoEntity> selectLike(SysUserInfoEntity userInfo) {
//
//        userInfoDao.selectLike(userInfo);
//
//
//
//        return userInfoDao.selectLike(userInfo);
//
//    }

    @Override
    public List<SysUserInfoVOEntity> selectLike(SysUserInfoVOEntity userInfoVO) {


        return userInfoDao.findLike(userInfoVO);
    }

    @Override
    public List<SysUserInfoVOEntity> findAll() {
        return userInfoDao.findAll();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAll(UserInfoForm vo) {

        System.out.println(vo);
        UserForm form = vo.getUserEntity();
        String email = form.getEmail();
        String mobile = form.getMobile();
        //获取前端新增的信息,设置账户密码，加盐，
        String password = form.getPassword();
        String username = form.getUsername();
        SysUserEntity userEntity = new SysUserEntity();


        //生成随机盐
        String salt = RandomStringUtils.randomAlphanumeric(20);
        userEntity.setSalt(salt); //保存盐
        userEntity.setPassword(new Sha256Hash(password,salt).toHex());

        //默认账户可用
        userEntity.setCreateUserId(getUserId());
        userEntity.setCreateTime(new Date());
        userEntity.setUsername(username);
        userEntity.setMobile(mobile);
        userEntity.setEmail(email);
        userEntity.setStatus(form.getStatus());
        userEntity.setStatusApp(form.getStatusApp());

        //存储账号和密码、存储user
        userDao.insert(userEntity);

        SysUserInfoEntity userInfoEntity = vo.getUserInfoEntity();

        //存储用戶詳細信息
        userInfoEntity.setUserId(userEntity.getUserId());
        userInfoDao.insert(userInfoEntity);

        //保存用户为委员角色
        SysUserRoleEntity userRole = new SysUserRoleEntity();
        userRole.setUserId(userEntity.getUserId());
        userRole.setRoleId("1");

        return userRoleService.save(userRole);
    }


    @Override
    public IPage<UserInfoVoEntity> listAllRemoveMe(Integer page, Integer limit, RoleSearchEntity search) {

        String userId = getUserId();

        Page<UserInfoVoEntity> pages = new Page<>(page,limit);

        return userInfoDao.listAllRemoveMe(pages,userId,search);
    }

    @Override
    public List<SysUserInfoEntity> findList() {

        return userInfoDao.findList();
    }

    @Override
    @Transactional
    public void updateByUserId(UserInfoForm vo) {

        UserForm userForm = vo.getUserEntity();
        SysUserEntity userEntity = new SysUserEntity();
        userEntity.setUserId(userForm.getUserId());
        userEntity.setUsername(userForm.getUsername());
        userEntity.setMobile(userForm.getMobile());
        userEntity.setStatus(userForm.getStatus());
        userEntity.setStatusApp(userForm.getStatusApp());
        //修改登录信息表  sys_user
        userDao.updateById(userEntity);

        //修改基本信息表
        SysUserInfoEntity userInfoEntity = vo.getUserInfoEntity();

        userInfoDao.update(userInfoEntity,new UpdateWrapper<SysUserInfoEntity>().eq("user_id",userForm.getUserId()));

    }

    @Override
    @Transactional
    public void deleteByUserId(String userId) {
        //删除账户表  sys_user
        userDao.deleteById(userId);
        //删除基本信息表
        userInfoDao.delete(new QueryWrapper<SysUserInfoEntity>().eq("user_id",userId));
        //删除角色关系表
        userRoleService.remove(new QueryWrapper<SysUserRoleEntity>().eq("user_id",userId));
    }
}
